import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:get/get.dart';

import '../../../bean/bean_train.dart';
import '../controller/course_sign_controller.dart';
import 'course_sign_list_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///课程简介
class CourseSignPage extends StatefulWidget {
  final BeanVideoCourse beanVideoCourse;
  final int trainId;

  const CourseSignPage({
    Key key,
    this.beanVideoCourse,
    this.trainId,
  }) : super(key: key);

  @override
  _CourseSignPageState createState() => _CourseSignPageState();
}

class _CourseSignPageState extends State<CourseSignPage> {
  CourseSignController _courseSignController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _courseSignController = Get.put(
        CourseSignController(widget.beanVideoCourse, widget.trainId),
        tag: "${widget.beanVideoCourse.id}");
    Future.delayed(Duration.zero, () {
      _courseSignController.initRequest();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 1),
      color: Colors.white,
      child: SingleChildScrollView(
        child: GetBuilder(
          id: "root",
          tag: "${widget.beanVideoCourse.id}",
          builder: (CourseSignController controller) {
            bool isLoading = controller.isLoading;
            if (isLoading) {
              return LoadingWidget();
            }

            return Column(
              children: [
                //签到统计信息
                buildSignStatWidget(controller),
                //签到组件
                buildSignWidget(controller),
              ],
            );
          },
        ),
      ),
    );
  }

//签到统计信息
  buildSignStatWidget(CourseSignController controller) {
    return GetBuilder(
      id: "sign-stat",
      tag: "${widget.beanVideoCourse.id}",
      builder: (CourseSignController controller) {
        AppCourseLineSignStatEntity signRecoderBean = controller.signStatEntity;
        if (signRecoderBean == null) {
          return SizedBox();
        }
        String statStr1 = "";
        String statStr2 = "";
        if (signRecoderBean != null) {
          statStr1 = signRecoderBean.statStr1 ?? "";
          statStr2 = signRecoderBean.statStr2 ?? "";
        }
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(height: 20),
            Container(
              decoration: BoxDecoration(
                color: Colors.grey[200],
                borderRadius: BorderRadius.all(Radius.circular(4)),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  GestureDetector(
                    child: Row(
                      children: [
                        Expanded(
                          child: Text("概览"),
                        ),
                        Icon(
                          Icons.arrow_forward_ios_sharp,
                          size: 12,
                        ),
                        SizedBox(width: 10),
                      ],
                    ),
                    onTap: () {
                      Get.to(
                        () => CourseSignAllListPage(
                          beanVideoCourse: widget.beanVideoCourse,
                          trainId: widget.trainId,
                        ),
                      );
                    },
                  ),
                  SizedBox(height: 10),
                  Row(
                    children: [
                      Image.asset(
                        "assets/images/3.0x/sign_sta_icon.png",
                        width: 14,
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Text(
                        "$statStr1",
                        style: TextStyle(fontSize: 12, color: Colors.grey[600]),
                      )
                    ],
                  ),
                ],
                crossAxisAlignment: CrossAxisAlignment.start,
              ),
              margin: EdgeInsets.only(left: 12, right: 5),
              height: 48,
              padding: EdgeInsets.only(left: 12),
              alignment: Alignment.centerLeft,
            )
          ],
        );
      },
    );
  }

  //签到组件
  buildSignWidget(CourseSignController controller) {
    AppCourseLineSignResponseEntity signResponseEntity =
        controller.signResponseEntity;
    if (!signResponseEntity.isOpen) {
      if (signResponseEntity.code != null && signResponseEntity.code == 402) {
        //培训已完成
        return SizedBox();
      }
      return Container(
        margin: EdgeInsets.all(12),
        padding: EdgeInsets.only(left: 10, right: 10, top: 14, bottom: 14),
        decoration: BoxDecoration(
          color: Colors.grey[200],
          borderRadius: BorderRadius.all(Radius.circular(4)),
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ClipOval(
              child: Container(
                color: Colors.red,
                child: Icon(
                  Icons.clear,
                  color: Colors.white,
                ),
              ),
            ),
            SizedBox(
              width: 12,
            ),
            Expanded(
              child: Text(signResponseEntity.message),
            ),
          ],
        ),
      );
    }

    return SizedBox(
      width: double.infinity,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 10),
          GetBuilder(
            id: "time-day",
            tag: "${widget.beanVideoCourse.id}",
            builder: (CourseSignController controller) {
              SignRecoderBean signRecoderBean = controller.signRecoderBean;
              String signUpTime = "";
              String signToTime = "";
              if (signRecoderBean != null) {
                signUpTime = signRecoderBean.signUpTime ?? "";
                signToTime = signRecoderBean.signToTime ?? "";
              }
              return Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: GestureDetector(
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.grey[200],
                          borderRadius: BorderRadius.all(Radius.circular(4)),
                        ),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text("开始学习"),
                            SizedBox(height: 4),
                            signToTime.isEmpty
                                ? Text(
                                    "未打卡",
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.grey[600]),
                                  )
                                : Row(
                                    children: [
                                      Icon(
                                        Icons.access_alarm,
                                        size: 14,
                                        color: Colors.blue,
                                      ),
                                      Text(
                                        "${signToTime.substring(10, 19)}已打卡",
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Colors.grey[600]),
                                      )
                                    ],
                                  ),
                          ],
                          crossAxisAlignment: CrossAxisAlignment.start,
                        ),
                        margin: EdgeInsets.only(left: 12, right: 5),
                        padding: EdgeInsets.only(left: 12),
                        height: 48,
                        alignment: Alignment.centerLeft,
                      ),
                      onTap: () {
                        controller.updateSinToClick();
                      },
                    ),
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        controller.updateSinOffClick();
                      },
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.grey[200],
                          borderRadius: BorderRadius.all(Radius.circular(4)),
                        ),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text("结束学习"),
                            SizedBox(height: 4),
                            signUpTime.isEmpty
                                ? Text(
                                    "未打卡",
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.grey[600]),
                                  )
                                : Row(
                                    children: [
                                      Icon(
                                        Icons.access_alarm,
                                        size: 14,
                                        color: Colors.blue,
                                      ),
                                      Text(
                                        "${signUpTime.substring(10, 19)}已打卡",
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Colors.grey[600]),
                                      )
                                    ],
                                  ),
                          ],
                          crossAxisAlignment: CrossAxisAlignment.start,
                        ),
                        margin: EdgeInsets.only(left: 12, right: 5),
                        height: 48,
                        padding: EdgeInsets.only(left: 12),
                        alignment: Alignment.centerLeft,
                      ),
                    ),
                  ),
                ],
              );
            },
          ),
          SizedBox(height: 60),
          GetBuilder(
            id: "time",
            tag: "${widget.beanVideoCourse.id}",
            builder: (CourseSignController controller) {
              SignRecoderBean signRecoderBean = controller.signRecoderBean;
              bool submitSignLoading = controller.submitSignLoading;
              String signUpTime = "";
              String signToTime = "";
              if (signRecoderBean != null) {
                signUpTime = signRecoderBean.signUpTime ?? "";
                signToTime = signRecoderBean.signToTime ?? "";
              }

              String buttonText = "签到";
              if (signToTime.isEmpty) {
                buttonText = "签到";
              } else if (signUpTime.isEmpty) {
                buttonText = "签退";
              } else {
                return SizedBox();
              }

              return GestureDetector(
                onTap: () {
                  if (signToTime.isEmpty) {
                    controller.signOnClick();
                  } else if (signUpTime.isEmpty) {
                    controller.signOffClick();
                  }
                },
                child: Container(
                  decoration: const BoxDecoration(
                    color: Colors.blue,
                    borderRadius: BorderRadius.all(Radius.circular(100)),
                    // 阴影的颜色，模糊半径
                    boxShadow: [BoxShadow(color: Colors.blue, blurRadius: 6)],
                  ),
                  width: 120,
                  height: 120,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      submitSignLoading
                          ? CircularProgressIndicator(
                              color: Colors.white,
                            )
                          : Text(
                              buttonText,
                              style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.w600,
                                  color: Colors.white),
                            ),
                      SizedBox(height: 6),
                      submitSignLoading
                          ? SizedBox()
                          : Text(controller.currentTimeStr,
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Colors.white.withAlpha(200))),
                    ],
                  ),
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}
